from numpy import *
from util import *

X2d = array([[ 1.8290571 , -1.73590059,  1.24248465, 2.88263353, -2.01186748,
               1.96518157, -1.93565787, -2.76630569,  0.10592241, -3.19874568,
               2.77369714, -2.29756079,  1.29010595, -0.76728233,  1.82182503,
               2.39701703,  0.73224671, -3.03450814, -1.14561687,  3.85039312,
               3.13170743, -2.06238037,  2.07574238, -2.77011599, -2.43368879,
               -2.40276404,  2.28903111, -1.47464774,  2.99666055,  2.51534108,
               -2.74108203, -3.80176311,  2.01647387, -3.01890761,  2.13636954,
               -2.26668094,  3.85257582,  2.14616894, -0.71513073, -2.35107546],
             [ 2.34822953,  -0.73272271,  2.26536247, 2.73435205, -1.39154131,
               0.55110039, -2.48528633, -4.23704741, -0.49291275, -2.03461486,
               1.14794456, -2.61464449,  2.44706273, -3.33761891,  1.85021528,
               0.62237424,  1.152404  , -1.75030497, -1.36869177,  0.86103013,
               0.51551839, -3.25987475, -0.99164307, -2.73344523, -2.05475336,
               -0.02626625,  3.67266857, -3.23969404,  1.05732863, -0.03480324,
               -2.16249447, -1.34010771,  2.31410424, -1.93999147,  2.73860486,
               -2.24319571,  2.69950384,  0.80206681, -2.19543213, -3.64817299]
             ], dtype=float).T

X2d2 = array([[ -1.81584244e+00,  -4.78779135e+00,   1.20153951e+00,
         -3.74426100e+00,   4.31791370e+00,  -4.26578107e+00,
         -4.71718293e+00,   4.85711897e-01,   2.93893320e+00,
          5.95589127e+00,  -5.23404039e+00,   3.87644984e+00,
          3.06823274e+00,   3.53747789e+00,   3.24455531e-01,
         -3.37937465e+00,  -3.16024181e+00,  -3.28397220e+00,
          4.44196180e+00,   4.41585530e+00,  -4.06639053e+00,
          3.69238146e+00,   2.09718176e+00,   2.85100791e+00,
          7.64266727e-01,  -1.92468506e-01,   4.55935736e+00,
          2.63422446e+00,   3.06022824e+00,  -4.47019798e+00,
          3.51613442e+00,  -2.98704414e+00,  -3.22237160e+00,
          5.39563340e-01,   9.36076640e-01,   5.07457070e-01,
         -4.34340238e+00,  -3.59167656e+00,  -2.36502492e+00,
          3.94610627e-01,  -4.21552514e+00,   1.99816317e+00,
          3.30186377e+00,  -3.99307497e+00,  -3.61433721e+00,
         -1.82029196e+00,  -2.68083836e+00,   6.79913361e-01,
         -4.43717360e+00,   1.63978787e+00,  -3.16946783e+00,
         -2.87963686e+00,  -2.75894351e+00,   1.20178366e+00,
         -3.62519711e+00,  -2.58707714e+00,   5.73229540e+00,
          3.08737782e+00,  -3.81507003e+00,  -5.20122552e-01,
          4.70711227e+00,  -2.30705822e+00,  -3.54448372e+00,
         -3.94471962e+00,   4.06497063e+00,  -5.23105177e+00,
          1.78323832e+00,  -1.85251197e+00,  -3.81160739e+00,
         -2.33905930e+00,  -2.35495361e+00,  -4.82868361e+00,
          3.75297124e+00,  -3.71936446e+00,   1.55531865e+00,
         -2.98014279e+00,   3.36826066e+00,   4.37736269e+00,
         -4.31990700e+00,   8.53558363e-01,  -2.05618489e+00,
          1.21397160e+00,   2.34238252e+00,  -3.98298974e+00,
         -3.08626982e+00,  -2.91847286e+00,   1.03880982e+00,
          4.54847771e+00,   2.00955223e+00,   2.48533819e+00,
         -2.74289214e+00,   9.83822093e-01,  -4.15050849e+00,
         -3.30939705e+00,   4.85887021e+00,   4.49787894e-01,
         -4.12657957e+00,  -4.62478948e+00,  -3.07078910e+00,
         -2.74646869e+00,   1.05812849e+00,   1.78986259e+00,
         -4.79864138e+00,  -4.80553563e+00,   1.48727066e+00,
         -2.45374942e+00,   2.71922023e+00,   4.29054891e+00,
          1.59340077e+00,   1.57860239e+00,   2.40295079e+00,
         -3.84625872e+00,  -3.56976903e+00,   3.92258025e-01,
          4.23420982e+00,  -2.82478296e+00,   4.14035313e+00,
          4.46354293e-01,  -2.90962872e+00,  -3.47910818e+00,
         -3.45169557e+00,   3.34975045e+00,  -3.89431046e+00,
         -3.92837851e+00,  -4.20249999e+00,   2.20479193e+00,
          3.25022296e+00,  -4.10255407e+00,   3.45803850e+00,
         -4.31809569e+00,  -2.97151041e+00,  -4.17434543e+00,
          2.63414222e+00,   3.21646113e+00,  -2.04398125e+00,
          4.14073602e+00,   1.11631086e+00,   3.62728423e-01,
         -3.73042632e+00,   3.18971974e+00,  -8.51154447e-01,
         -3.40943976e+00,  -1.67546736e+00,  -3.66851387e+00,
          4.56625471e+00,  -3.41510988e+00,  -4.64441030e-01,
         -1.81802388e+00,  -3.95272931e+00,  -4.77827716e+00,
          3.22736918e+00,   1.58306807e+00,  -3.60820781e+00,
         -2.23260087e+00,  -3.24891868e+00,   2.93535456e+00,
          1.67304971e-01,   5.10351828e+00,  -2.38377060e-01,
          3.56898906e+00,  -3.73529988e+00,   2.30113046e+00,
         -3.06273807e+00,  -3.04856465e+00,   2.02748028e+00,
         -3.43919650e+00,   5.05252504e+00,  -2.23169377e+00,
          2.95535745e-01,   8.96420812e-01,   1.06217382e+00,
          2.99361841e+00,   6.36150430e+00,   1.15378242e+00,
          1.96135128e+00,   3.04787375e+00,   1.02064768e+00,
          8.91724354e-01,  -4.78179071e+00,   7.88107623e-01,
          1.56390348e+00,  -1.56116778e+00,  -4.22443404e+00,
          2.86842893e+00,   4.50395039e+00,   3.60074722e+00,
          1.39805575e+00,  -2.01060963e+00,   8.77508081e-01,
          2.50248372e+00,   1.74814750e-01,   2.10419182e+00,
          1.54737036e+00,  -1.82209857e+00,   3.37266121e+00,
          1.23925945e+00,   3.77937706e+00,  -3.57407945e+00,
          1.58385375e-01,  -4.55863096e+00,  -3.90849986e+00,
          1.18857996e+00,  -2.19634328e+00,  -4.05372858e+00,
          5.45627450e-01,  -4.58373028e+00,  -3.08091186e+00,
          3.51305964e+00,   3.56501701e+00,   2.41198732e+00,
          2.77106099e+00,  -2.66782842e+00,   2.08491577e-01,
          2.73601029e+00,  -3.96992105e+00,   4.58450986e+00,
         -2.66505671e+00,   2.74623844e+00,  -3.97556652e+00,
          6.04908613e-01,  -3.71682532e+00,   3.60864806e-01,
         -2.62175885e+00,  -4.51746100e+00,  -4.47519519e+00,
         -1.46307476e+00,  -2.42163494e+00,  -5.70269057e+00,
         -4.19390278e+00,  -3.84920088e+00,   9.67155819e-01,
         -4.40973469e+00,   2.66330324e+00,   4.68737027e+00,
          1.32373381e+00,  -2.40010431e+00,   4.08974622e-01,
         -4.59811158e+00,  -1.80682023e+00,  -3.72919859e+00,
         -3.12760178e+00,  -3.74539599e+00,  -3.64902335e+00,
         -3.57568279e+00,   2.13252243e+00,  -2.51181392e+00,
          4.16395836e+00,   3.90614195e-01,   1.40520867e+00,
          1.17418303e+00,   8.15638979e-01,   6.84257568e-01,
         -3.82235830e+00,   2.03146835e-01,  -3.32737562e+00,
         -3.36422354e+00,  -2.62530958e+00,  -4.72499095e+00,
         -3.89659062e+00,  -3.67048968e+00,  -3.84770989e+00,
          3.15509692e-01,  -4.28347012e+00,  -4.59239347e+00,
         -3.02039108e+00,   3.27442870e+00,  -1.51678157e+00,
          2.53259514e+00,  -4.76142673e+00,  -3.63435276e+00,
         -9.65049349e-01,  -3.36237904e+00,   2.98842092e-01,
         -4.18227035e+00,   1.70382017e+00,   2.58225250e+00,
         -1.54530324e+00,   4.55241156e+00,   1.03951351e+00,
         -1.90538810e+00,  -2.48307814e+00,  -3.61023392e+00,
          2.08703844e+00,  -2.59152313e+00,   2.09451733e+00,
         -2.49436191e+00,  -2.23762565e+00,  -4.01745808e+00,
         -3.88934286e+00,  -4.07353668e+00,   3.34311101e-01,
          6.11915929e+00,  -2.73016671e+00,   2.77526081e+00,
         -2.86330468e+00,  -5.16360312e+00,   4.42645367e+00,
         -4.23966284e+00,  -4.82948290e+00,   3.59654109e+00],
       [ -5.91033880e+00,   2.02005721e+00,   4.28425469e+00,
         -3.88104591e+00,  -2.41098495e+00,   1.95160961e+00,
          1.21978801e+00,   3.65565187e+00,   2.15338360e+00,
         -1.27360124e+00,  -5.62040180e+00,   3.97789757e-01,
         -1.10466409e+00,   2.95608308e-01,   5.00598964e+00,
         -4.01651856e+00,  -3.93012262e+00,  -4.05371604e+00,
         -3.37508363e+00,   5.83768304e-01,   1.53555234e+00,
         -1.57936661e+00,  -2.37473862e+00,  -6.99452998e-01,
          4.24187851e+00,   5.64488155e+00,  -5.75502087e-03,
         -1.44185295e+00,  -8.66527240e-01,   2.86564860e+00,
         -2.20338526e+00,  -3.39868423e+00,  -1.99306929e+00,
          3.85381025e+00,   4.11668997e+00,   6.25882208e+00,
          2.82546528e+00,   2.11327658e+00,  -3.76519620e+00,
         -8.38367466e-01,  -5.02307545e+00,  -3.45268559e+00,
         -1.02470780e+00,  -4.75980511e+00,   2.63867751e+00,
         -4.37134283e+00,  -3.56664647e+00,   5.83704050e+00,
          2.13254802e+00,  -1.87109269e+00,   1.81709209e+00,
          1.80312427e+00,  -3.32544769e+00,   4.42777046e+00,
         -3.98646312e+00,  -4.50150330e+00,  -6.83107881e-01,
         -2.54545469e+00,  -5.34836187e+00,   5.00914438e+00,
         -2.72966887e+00,  -5.16996532e+00,   1.96167083e+00,
         -5.52311357e+00,  -7.24822091e-01,  -2.64662374e+00,
          4.19099600e+00,  -4.45319253e+00,  -3.13106711e+00,
         -5.41672975e+00,  -3.01452776e+00,  -4.62388685e+00,
         -6.81996228e-01,   1.68397444e+00,  -1.81945144e+00,
         -4.71339598e+00,  -2.97680914e+00,  -2.79073491e+00,
          1.78604836e+00,   5.08790525e+00,  -3.01928927e+00,
          1.97401324e+00,   4.80149681e+00,  -3.89461996e+00,
         -3.75049691e+00,   1.72533482e+00,   6.70411298e+00,
         -1.76510645e+00,  -3.01316323e+00,   4.33732776e+00,
         -2.69145095e+00,   1.50488451e-01,   2.17468772e+00,
         -4.00055071e+00,   9.95540103e-01,   6.51776463e+00,
         -3.89364057e+00,  -3.07401658e+00,  -4.86238309e+00,
         -4.60429247e+00,   4.45819531e+00,   6.35032286e+00,
         -3.95039933e+00,   2.15837664e+00,  -1.72342368e+00,
         -2.68776907e+00,  -2.78908309e+00,  -1.21858952e-01,
         -2.12040351e+00,  -2.15584767e+00,  -1.93306576e+00,
          2.13882415e+00,   9.55914365e-01,   1.87461451e+00,
         -8.91477338e-02,   2.03456275e+00,   1.12543331e+00,
          4.73234205e+00,  -3.94894698e+00,   1.72686835e+00,
         -5.32159529e+00,  -2.46571905e+00,   1.41574821e+00,
          1.40979911e+00,  -3.47659752e+00,  -1.45775804e+00,
         -4.66161383e+00,   2.06450812e+00,  -3.85906266e+00,
         -4.60501592e+00,  -4.10327778e+00,   2.10210087e+00,
         -2.97429561e+00,   3.10274542e-01,  -4.97828802e+00,
         -1.53687799e+00,   5.60504812e+00,   4.02052199e+00,
          2.61094678e+00,  -1.87542804e-01,   4.91641303e+00,
         -4.28224640e+00,  -4.15560759e+00,  -4.06160911e+00,
         -4.30513266e-01,  -2.59854769e+00,   5.15635865e+00,
         -3.91126053e+00,   2.42170705e+00,   1.68861522e+00,
          8.00970548e-01,   5.85049663e+00,   2.37814536e+00,
         -3.80340143e+00,  -1.94441935e+00,   1.03056951e-01,
          5.12715803e+00,  -1.71623554e+00,   4.79210445e+00,
         -1.23687935e+00,   1.02971330e+00,   2.06780227e+00,
         -3.90140434e+00,   1.89336011e+00,  -3.71311563e+00,
         -1.78676522e+00,  -5.50184258e-01,  -4.03260172e+00,
         -3.69484932e+00,   3.81765370e+00,   3.66180752e+00,
         -3.20611070e+00,   3.80232396e-02,   4.03568195e-01,
         -9.59590040e-01,   9.36764902e-01,   4.44732504e+00,
         -4.37854146e+00,  -4.79886901e+00,   7.55200192e-01,
         -2.88500018e+00,   4.55583619e+00,  -4.04804334e+00,
         -6.47143187e-01,   1.22199557e+00,  -5.96956697e-01,
          5.00651662e+00,  -2.97259147e+00,   5.46730782e+00,
          8.08388873e-01,   3.36598235e+00,  -1.98592194e+00,
          3.72129174e-01,  -3.95177038e+00,   9.49373882e-01,
          5.61193086e+00,  -1.14345257e+00,   1.65804544e+00,
          5.39642020e+00,  -4.55182860e+00,  -4.16670651e+00,
         -4.18823978e-01,  -3.58041791e+00,  -5.08770660e+00,
          6.14473308e+00,  -5.46767381e+00,   2.33169243e+00,
         -8.11700263e-01,  -2.69353563e+00,  -1.51787110e+00,
          7.93700842e-01,  -3.63252239e+00,   4.62987003e-01,
         -5.84421909e-01,   2.20360960e+00,  -9.20498398e-02,
         -4.02286257e+00,  -1.47950492e+00,   1.88426416e+00,
          3.81778027e+00,  -2.82514284e+00,   4.27411694e+00,
         -5.41877911e+00,  -4.07271549e+00,   2.67036045e+00,
         -3.58924315e+00,  -4.06788285e+00,  -3.19933845e+00,
          2.63725041e+00,  -3.76351194e+00,   8.90721774e-02,
         -3.06269895e+00,   3.64373732e+00,   1.08632553e+00,
          4.98971485e+00,  -4.70731696e+00,   4.82136264e+00,
         -3.68447977e+00,  -4.26330174e+00,   1.82265470e+00,
         -5.35264012e+00,  -4.75226461e+00,  -4.37618113e+00,
         -5.20354644e+00,   5.29709299e+00,  -4.32670709e+00,
         -1.38942956e+00,   1.44053354e+00,   4.26241734e+00,
          8.80503050e-01,  -1.22653711e+00,   4.80872553e+00,
          1.95515214e+00,   4.05622258e+00,  -3.71990529e+00,
          9.02398563e-01,  -4.92861178e+00,   1.61902254e+00,
         -5.07344978e+00,   1.82885230e+00,  -5.10641227e+00,
          3.12948556e-01,   2.70934797e+00,  -5.91730704e+00,
         -4.64141190e+00,  -2.04371850e-01,  -4.48396229e+00,
         -4.04302113e+00,   1.68002585e+00,  -4.46722167e+00,
         -2.10057201e+00,   2.64966984e+00,   5.13601101e+00,
         -5.20471283e+00,  -5.86399853e-01,   5.43433015e+00,
         -4.59540228e+00,  -2.03520541e+00,   7.24714129e-01,
         -4.10709026e+00,  -4.64023887e+00,   2.49976145e+00,
          6.88529614e+00,  -4.73926723e+00,   4.13189280e-01,
         -2.56480339e+00,  -3.90084470e+00,   1.55278109e+00,
          1.40913078e+00,   2.54722721e+00,   5.46450144e+00,
         -3.40366732e-01,  -2.73126368e+00,  -4.59968883e-01,
         -4.13003901e+00,   2.68142070e+00,  -9.63183564e-01,
         -4.01577304e+00,  -5.43892020e+00,  -1.88593784e+00]]).T

def loadDigits():
    X = zeros((1000, 784), dtype=float)
    Y = zeros((1000,), dtype=int)
    n = 0

    f = open('digits', 'r')
    for line in f:
        vals = [int(v) for v in line.split()]
        Y[n] = vals[0] % 10
        for i in range(len(vals)/2-1):
            X[n, vals[i*2+1]] = float(vals[i*2+2])
        X[n,:] = X[n,:] / linalg.norm(X[n,:])
        n = n + 1
    f.close()

    return (X,Y)


def makeKPCAdata():
    a = randn(200,2)
    ida = sum(a * a, axis=1) < 5
    b = randn(1000,2) * 2
    idb = sum(b * b, axis=1) > 10
    return(a[ida,:], b[idb,:])
